package com.reger.canal.client;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串 下划线与驼峰转化工具类
 * 
 * @author leige
 *
 */
public abstract class StringUnderlineCamelUtils {
	public static final char UNDERLINE = '_';
	public static final Pattern UNDERLINE_PATTERN = Pattern.compile("_");

	/**
	 * 转换为下划线
	 * @param param
	 * @return
	 */
	public static String toUnderline(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}
	/**
	 * 转换为驼峰
	 * @param param
	 * @return
	 */
	public static String toCamel(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (c == UNDERLINE) {
				if (++i < len) {
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}
	/**
	 * 转换为驼峰
	 * @param param
	 * @return
	 */
	public static String toCamel2(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		StringBuilder sb = new StringBuilder(param);
		Matcher mc = UNDERLINE_PATTERN.matcher(param);
		int i = 0;
		while (mc.find()) {
			int position = mc.end() - (i++);
			sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
		}
		return sb.toString();
	} 
}
